﻿$(function () {
    
    $(function () {
        
        $(document).on('pagebeforeshow', '#select-date-check-in', function () {

            // refresh datebox to update ui
            $('#select-date-check-in .my-checkin-date').trigger('datebox', { 'method': 'doset' });

            // bind datebox event
            $('#select-date-check-in .my-checkin-date').bind('datebox', function (e, p) {
                if (p.method === 'close') {
                    e.stopImmediatePropagation()

                    // get selected data

                    var date_box = $('#select-date-check-in .my-checkin-date').datebox('getTheDate');
                    var weekdays = ["CN", "Thứ 2", "Thứ 3", "Thứ 4", "Thứ 5", "Thứ 6", "Thứ 7"];

                    var weekday = weekdays[date_box.getDay()];
                    var month = date_box.getMonth() + 1;
                    var date = date_box.getDate();
                    var year = date_box.getFullYear();
                    $(".divNhanPhong .day-month .day").html(weekday);
                    $(".divNhanPhong .day-month .month").html("T" + month);
                    $(".divNhanPhong .date").html(date);
                    $(".ArrivalDate").val(toDateShortYMD(date_box));
                    if (toDateShortYMD(date_box) >= $(".DepartureDate").val()) {
                        var date_nex = new Date($(".ArrivalDate").val());
                        date_nex.setDate(date_box.getDate() + 1);
                        weekday = weekdays[date_nex.getDay()];
                        month = date_nex.getMonth() + 1;
                        date = date_nex.getDate();
                        year = date_nex.getFullYear();
                        $(".divTraPhong .day-month .day").html(weekday);
                        $(".divTraPhong .day-month .month").html("T" + month);
                        $(".divTraPhong .date").html(date);
                        $(".DepartureDate").val(toDateShortYMD(date_nex));
                    }
                    // Change page
                    $.mobile.changePage("#main-page", { transition: "slide", reverse: true });
                }
            });
        });
    });
    $(function () {
        /*checkin and check out room*/
        // refresh datebox to update ui
        $(document).on('pagebeforeshow', '#select-date-check-out', function () {
            var departureDate = new Date($(".DepartureDate").val());
            $('#select-date-check-out .my-checkout-date').datebox('setTheDate', departureDate);
            var temp = new Date();
            var check_in_date = new Date($(".ArrivalDate").val());
            diff = parseInt((check_in_date - temp) / (1000 * 60 * 60 * 24));

            // if current date           
            if (toDateShortYMD(check_in_date) == toDateShortYMD(departureDate)) {
                diffstrt = (diff * -1) - 1; // If you want a minimum of 1 day between, make this -2 instead of -1
            } else {
                // other
                diffstrt = (diff * -1) - 2;
            }
            $('#select-date-check-out .my-checkout-date').data('mobileDatebox').options.minDays = diffstrt;
            $('#select-date-check-out .my-checkout-date').datebox('applyMinMax');
            $('#select-date-check-out .my-checkout-date').trigger('datebox', { 'method': 'doset' });
            // bind datebox event
            $('#select-date-check-out .my-checkout-date').bind('datebox', function (e, p) {
                if (p.method === 'close') {
                    e.stopImmediatePropagation()

                    // get selected data
                    var date_box_out = $('#select-date-check-out .my-checkout-date').datebox('getTheDate');
                    var weekdays = ["CN", "Thứ 2", "Thứ 3", "Thứ 4", "Thứ 5", "Thứ 6", "Thứ 7"];

                    var weekday = weekdays[date_box_out.getDay()];
                    var month = date_box_out.getMonth() + 1;
                    var date = date_box_out.getDate();
                    var year = date_box_out.getFullYear();
                    $(".divTraPhong .day-month .day").html(weekday);
                    $(".divTraPhong .day-month .month").html("T" + month);
                    $(".divTraPhong .date").html(date);
                    $(".DepartureDate").val(toDateShortYMD(date_box_out));
                    // Change page
                    $.mobile.changePage("#main-page", { transition: "slide", reverse: true });
                }
            });
        });
    });
});
function toDateShortYMD(date) {
    return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
} 
function goPage(page) {
    $.mobile.changePage("#" + page, { transition: "slide", reverse: true });
}
function SelectCity(control) {
    var id_city = $(control).attr('id_city');
    var text_city = $(control).attr('text');
    $(".hdIDTinhThanhSelected").val(id_city);
    $("#txt_city").html(text_city);
    $(".hdNameCity").val(text_city);
}
function Increase(id, val_max, control, control_decrease) {
    $(control_decrease).removeClass('ui-disabled');
    var val = $("#" + id).html();
    val++;
    $("#" + id).html(val.toString());
    var id_server = $("#" + id).attr("id_server");
    $("." + id_server).val(val);
    if (val > val_max || val_max == val) {
        $(control).addClass('ui-disabled');
    }
}
function Decrease(id, val_min, control, control_increase) {
    $(control_increase).removeClass('ui-disabled');
    var val = $("#" + id).html();
    val--;
    if (val < val_min || val_min == val) {
        $(control).addClass('ui-disabled');
    }
    $("#" + id).html(val.toString());
    var id_server = $("#" + id).attr("id_server");
    $("." + id_server).val(val);
}
function checkSubmit() {
    if (!$(".hdIDTinhThanhSelected").val() || ($(".hdIDTinhThanhSelected").val()).lenght == 0) {
        alert("Vui lòng chọn thành phố!");
        return false;
    } 
}
function LoadContinuesListHotel(control) {
    var page = $(control).attr("p");
    var id_city = $(control).attr("id_city");
    $("html").addClass("ui-loading");
    $.ajax({
        type: "POST",
        url: "ajax.aspx/loadContinuesListHotels",
        data: JSON.stringify({ 'page': page, 'id_city': id_city }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $("#search-page .result-section").append(data["d"][0]);
            $(control).attr("p", parseInt(page) + 1);
            if (data["d"][1]==1) {
                $(".continue-button-section").hide();
            }
            $("html").removeClass("ui-loading");
            // Do something interesting here.
        }
    });
}
function LoadContinuesListHotelAdd(control,json_start, json_price, dangKM, optionSX, nameSearch) {
    var page = $(control).attr("p");
    var id_city = $(control).attr("id_city");
    $("html").addClass("ui-loading");
    $.ajax({
        type: "POST",
        url: "ajax.aspx/loadContinuesListHotelsAdd",
        data: JSON.stringify({ 'page': page, 'id_city': id_city, 'json_start': json_start, 'json_price': json_price, 'dangKM': dangKM, 'optionSX': optionSX, 'nameSearch': nameSearch }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $("#search-page .result-section").append(data["d"][0]);
            $(control).attr("p", parseInt(page) + 1);
            if (data["d"][1] == 1) {
                $(".continue-button-section").hide();
            }
            $("html").removeClass("ui-loading");
            // Do something interesting here.
        }
    });
}
function LoadContinuesListImageAndVideo(control) {
    var p= $(control).attr("p");
    var c = $(control).attr("c");
    $("html").addClass("ui-loading");
    for (var i = p; i < p + 10; i++) {
        if (i < c || i==c) {
            $("#img_d_" + i).show();
            $(control).attr("p",i)
        }
    }
    p = $(control).attr("p");
    if (p == c) {
        $(control).hide();
    }
    $("html").removeClass("ui-loading");
}
function HideAndShow(control, id_KS, id_LoaiPhong, type) {
    var id_hide = $(control).attr("id_hide");
    if ($(control).attr("show") == 1) {
        $("#" + id_hide).slideUp('slow');
        $(control).attr("show", 0);
        $(control).html($(control).attr("showtext"));
       
    } else {
        $("#" + id_hide).show('slide');
        $(control).attr("show", 1);
        $(control).html($(control).attr("hidetext"));
        if (type == 1) {
            LoadDetailLoaiPhong(id_hide, id_KS, id_LoaiPhong);
        }
    }
    
}

function LoadContinuesListArticles(control) {
    var page = $(control).attr("p");
    $("html").addClass("ui-loading");
    $.ajax({
        type: "POST",
        url: "ajax.aspx/LoadContinuesListArticles",
        data: JSON.stringify({ 'page': page}),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $("#listArticlesUL").append(data["d"][0]);
            $(control).attr("p", parseInt(page) + 1);
            if (data["d"][1] == 1) {
                $(".continue-button-articles").hide();
            }
            $("html").removeClass("ui-loading");
            // Do something interesting here.
        }
    });
}
function LoadContinuesListAssess(control,id) {
    var page = $(control).attr("p");
    $("html").addClass("ui-loading");
    $.ajax({
        type: "POST",
        url: "ajax.aspx/LoadContinuesListAssess",
        data: JSON.stringify({ 'page': page, 'id': id }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $(".divReviews-items-section").append(data["d"][0]);
            $(control).attr("p", parseInt(page) + 1);
            if (data["d"][1] == 1) {
                $(".reviewcontent-button-section").hide();
            }
            $("html").removeClass("ui-loading");
            // Do something interesting here.
        }
    });
}
function LoadDetailLoaiPhong(control, id_KS, id_LoaiPhong) {
    $("html").addClass("ui-loading");
    $.ajax({
        type: "POST",
        url: "ajax.aspx/LoadDetailLoaiPhong",
        data: JSON.stringify({ 'id_KS': id_KS, 'id_LoaiPhong': id_LoaiPhong }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
           // alert(control);
            $("#" + control).html(data["d"]);
            $("html").removeClass("ui-loading");
        }
    });
}